import random

import pygame

from actor.mine_boom import Boom, Mine
from dialog.final_dialog import FinalTask2StartDialog, FinalTaskFailDialog
from scene.task.stage_one import TaskStatus
from scene.task.stage_two.task1_find_weapon import FindWeapon

"""
任务2类
"""
class FightAndFind(FindWeapon):

    def __init__(self, xiao_tie, japanese, mines_group, prizes_group, font):
        """
        初始化
        :param xiao_tie:
        :param japanese:
        :param mines_group:
        :param prizes_group:
        :param font:
        """
        super().__init__(xiao_tie, japanese, mines_group, font)
        self.sound1 = pygame.mixer.Sound('resources/sound/爆炸声.wav')
        self.prizes_group = prizes_group
        self.count = 0

    def init_dialog(self):
        """
        对话初始化
        :return:
        """
        self.task_dialog = FinalTask2StartDialog(self.font)
        self.win_dialog = None
        self.fail_dialog = FinalTaskFailDialog(self.font)

    def do_collide_group(self):
        """
        实现精灵碰撞检测
        :return:
        """
        is_collide_japanese = pygame.sprite.spritecollide(self.xiao_tie, self.japanese, False)
        is_collide_mines_group = pygame.sprite.spritecollide(self.xiao_tie, self.mines_group, True)
        is_collide_mines = pygame.sprite.spritecollide(self.xiao_tie, self.mines, True)
        is_collide_prizes = pygame.sprite.spritecollide(self.xiao_tie, self.prizes_group, True)
        collide_japanese = pygame.sprite.groupcollide(self.mines, self.japanese, True, True)
        for wall in collide_japanese:
            self.sound1.play()
            self.create_boom(wall.rect.x - wall.rect.width,
                               wall.rect.y - wall.rect.height)
            self.count += 1
            if self.count == 4:
                self.xiao_tie.set_count(self.xiao_tie.boom_count + 4)
                self.task_status = TaskStatus.win_dialog

        if is_collide_mines_group or is_collide_mines or is_collide_prizes:
            self.sound.play()
            if is_collide_prizes:
                self.xiao_tie.hp += 50
                self.xiao_tie.shoot_count += 10
                self.xiao_tie.score += 1
            else:
                self.xiao_tie.boom_count += 1
        if is_collide_japanese:
            if self.xiao_tie.time_stamp == 0:
                self.xiao_tie.hp -= random.randint(25, 50)
                self.xiao_tie.time_stamp = 10
            else:
                self.xiao_tie.time_stamp -= 1
            if self.xiao_tie.hp <= 0:
                self.task_status = TaskStatus.fail_dialog

    def do_self_task(self, key_down, key_list):
        """
        任务具体执行内容
        :param key_down:
        :param key_list:
        :return:
        """
        self.do_collide_group()
        if key_down and key_list[pygame.K_SPACE]:
            if self.xiao_tie.boom_count > 0:
                self.create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)
                self.xiao_tie.boom_count -= 1

    def create_boom(self, x, y):
        """
        继承父类
        :param x:
        :param y:
        :return:
        """
        super().create_boom(x, y)

    def create_mine(self, x, y):
        """
        继承父类
        :param x:
        :param y:
        :return:
        """
        super().create_mine(x, y)

    def draw_task(self, current_surface, x, y):
        """
        继承父类
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        super().draw_task(current_surface, x, y)




